package org.standard.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.standard.commons.exception.InfrastructureException;
import org.standard.commons.utils.DAO;
import org.standard.sys.model.SysCode;
import org.standard.sys.model.SysLog;
import org.standard.sys.service.SysCodeService;

/**
 * 该类用于处理系统代码
 * 
 * @author wang zitao
 * 
 */

public class SysCodeServiceImpl implements SysCodeService {
	private static final char SUCCESS = '1';  //修改成功
	private static final char FAIL = '0';    //修改失败
	private static final char REPEAT = '2';    //存在重复数据
	private DAO dao;

	public DAO getDao() {
		return dao;
	}

	public void setDao(DAO dao) {
		this.dao = dao;
	}

	/**
	 * 查找系统代码主分类
	 * 
	 * @exception InfrastructureException
	 * @return
	 */
	@Override
	public List<SysCode> searchAllTopCode() {
		List codelist = dao.find("from  SysCode where codeNoParent is null or codeNoParent='' order by codeOrder");
		if (codelist.size() > 0) {
			return codelist;
		} else {
			return new ArrayList();
		}
	}
	
	
	/**
	 * 查找系统代码主分类的编号和名称
	 * 用于添加二级分类代码
	 * 
	 * @exception InfrastructureException
	 * @return
	 */
	@Override
	public List searchAllTopCodeNo() {
		List codenos = dao.find("from SysCode where codeNoParent is null or codeNoParent='' order by codeOrder");
		if (codenos.size() > 0) {
			return codenos;
		} else {
			return new ArrayList();
		}
	}
	
	/**
	 * 查找系统主分类代码的编号和名称
	 * 
	 * @exception InfrastructureException
	 * @return
	 */
	@Override
	public SysCode searchTopCodeByNo(String codeNo) {
		List codelist = dao.find("from SysCode where codeNo='"+codeNo+"'");
		if (codelist.size() > 0) {
			SysCode code=(SysCode)codelist.get(0);
			return code;
		} else {
			return new SysCode();
		}
	}

	/**
	 * 根据ID查找系统代码
	 * 
	 * @exception InfrastructureException
	 * @return
	 */
	@Override
	public SysCode searchCodeById(String codeId) {
		List codelist = dao.find("from SysCode where codeId='"+codeId+"'");
		if (codelist.size() > 0) {
			SysCode code=(SysCode)codelist.get(0);
			return code;
		} else {
			return new SysCode();
		}
	}
	
	/**
     * 通过系统编号查找系统分类代码
     * 用于编辑代码
	 * @author wang zitao
	 * @prama codeno本级代码编号
	 * @return 代码信息
	 */

	@Override
	public List<SysCode> searchCodeByNo(String codeno,String codeNoParent) {
		String hql="";
		List codelist=null;
		if(codeNoParent!=null&&!"".equals(codeNoParent)){
			hql="from SysCode where codeNo='"+codeno+"' and codeNoParent='" + codeNoParent+"' order by codeOrder";
			codelist = dao.find(hql);
		}
		else{
			hql="from SysCode where (codeNo =? and codeNoParent='') or (codeNo =? and codeNoParent is null) order by codeOrder";
			codelist = dao.find(hql,new Object[]{codeno,codeno});
		}
		if (codelist.size() > 0) {
			return codelist;
		} else {
			return new ArrayList();
		}
	}

	
	/**
     * 通过传入的上级系统代码编号查找该代码下的所有下级代码
	 * @author wang zitao
	 * @prama codeno本级代码编号，在查询时与副代码相对应
	 * @return 代码信息列表
	 */

	@Override
	public List<SysCode> searchNextCodeByNo(String codeno) {
		String hql="";
		if(codeno!=null&&!"".equals(codeno)){
			hql="from SysCode where codeNoParent='" + codeno+"' order by codeOrder";
		}
		List codes = dao.find(hql);
		if (codes.size() > 0) {
			return codes;
		} else {
			return new ArrayList();
		}
	}
	
	
	/**
	 * 修改代码信息
	 * 
	 * @param sysCode
	 *            SysCode对象
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wang zitao
	 */
	@Override
	public char modifyCode(SysCode sysCode) {
		try {
			dao.update(sysCode);
			return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}

	/**
	 * 添加系统代码
	 * 
	 * @param sysCode
	 *            SysCode对象
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wangzt
	 */
	@Override
	public char createCode(SysCode sysCode) {
		String  message="";
		try {
			List sysCodelist=dao.find("from SysCode where (codeNo=? and codeNoParent is null)" +
					" or (codeNo=? and codeNoParent ='') or (codeName=? and codeNoParent is null) or (codeName=? and codeNoParent ='')",
					 new Object[]{sysCode.getCodeNo(),sysCode.getCodeNo(),sysCode.getCodeName(),sysCode.getCodeName()});
			if(sysCodelist.size()>0){
				  return REPEAT;
			}
			sysCode.setCodeFlag("0");
			String codeId = (String) dao.save(sysCode);
			return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}
	
	/**
	 * 添加二级系统代码
	 * 
	 * @param sysCode
	 *            SysCode对象
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wangzt
	 */
	@Override
	public char createNextCode(SysCode sysCode) {
		String  message="";
		//查找是否有代码重复的
		try {
			List sysCodelist=dao.find("from SysCode where (codeNo=? and codeNoParent=?) or (codeName=? and codeNoParent=?) ",
					new Object[]{sysCode.getCodeNo(),sysCode.getCodeNoParent(),sysCode.getCodeName(),sysCode.getCodeNoParent()});
			if(sysCodelist.size()>0){
				  return REPEAT;
			}
			sysCode.setCodeFlag("0");
			String codeId = (String) dao.save(sysCode);
			return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}

	/**
	 * 删除主代码类
	 * 
	 * @param codeNo
	 *            要删除的代码编号
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wangzt
	 */
	@Override
	public char deleteCode(String codeId,String codeNo) {
		try {
				dao.updateBulk("delete from SysCode where (codeNo=? and codeNoParent is null) or (codeNo=? and codeNoParent='') or codeNoParent=?", new Object[]{codeNo,codeNo,codeNo});
				return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}
	
	
	
	/**
	 * 删除二级代码类
	 * 
	 * @param codeId
	 *            要删除的代码序号
	 * @return char SUCCESS OR FAIL 成功或者失败
	 * @author wangzt
	 */
	@Override
	public char deleteNextCode(String codeId) {
		try {
		 dao.delete(SysCode.class, codeId);
		return SUCCESS;
		} catch (InfrastructureException e) {
			return FAIL;
		}
	}
}
